<?php

namespace Common\Api;
class BookGameApi {
	/**
	 * 通过比赛id与班级id查询本班在本场比赛关于各图书的已读未读信息
	 * @param int $bookGameId	比赛id
	 * @param int $classId		班级id
	 * @return Ambigous <\Think\mixed, boolean, multitype:, unknown, mixed, object>
	 */
    public static function getReadOrNotByGameIdAndClassId($bookGameId,$classId){
		//查询得到参与该比赛的学生人数
    	$map['classid']		=	$classId;
    	$map['book_game_id']=	$bookGameId;
    	$studentCount		=	M('ClassGame')->where($map)->getField('studentcount');
    	//对于每本书已读未读
    	$data	=	M('ClassGameRelation cgr')
    				->join('primary_book_info bi ON cgr.bookid=bi.id','LEFT')
			    	->where($map)
			    	->field('bookname,read_times,('.$studentCount.'-read_times) not_read')
			    	->select();
    	return $data;
    }
    
    /**
     * 通过比赛id与班级id查询本班在本场比赛的学生已读次数统计信息
     * @param int $bookGameId	比赛id
     * @param int $classId		班级id
     * @return Ambigous <\Think\mixed, boolean, multitype:, unknown, mixed, object>
     */
    public static function getReadTimeByGameIdAndClassId($bookGameId,$classId){
    	//统计读了一本书 2本书 。。。(time)本书的人数(cnt)
    	$map['s.classid']		=	$classId;
    	$map['bgr.book_game_id']=	$bookGameId;
    	$studentGameModel	=	M('BookGameRelation bgr')
    							->join('primary_student s ON bgr.studentid=s.id','LEFT');
    	$data	=	$studentGameModel
    				->where($map)
    				->field('bgr.have_read_count times,count(*) cnt')
			    	->group('bgr.have_read_count')
    				->select();
    	return $data;
    }
    /**
     * 	查询学生在1次读书比赛中已读，未读，在读数量
     * @param $bookGameId  读书比赛ID
     * @param $studentId   学生ID
     * @return array       返回查询的学生读书本数数组
     */
    public static function getReadTimeByGameIdAndStudentId($bookGameId,$studentId){
    	$map['book_game_id']  =  $bookGameId;
    	$map['studentid']     =  $studentId;
//     	$bookGame             =  M('BookGame');
//     	$bookGameRst          =  $bookGame->find($bookGameId);
//     	$bookGameRstArr       =  explode(',', $bookGameRst['bookids']);//图书总数
//     	$bookGameRelation     =  M('BookGameRelation');
//     	$bookGameRelationRst  =  $bookGameRelation->where($map)->find();
//     	$map['isready']       =  1;//未开始
//     	$booklistGameRelation =  M('BooklistGameRelation');
//     	$booklistGameRelationRst  =  $booklistGameRelation->where($map)->select();
//     	$bookRst              =  array_column($booklistGameRelationRst, 'bookid');
//     	$bookGameRelationRst['not_read_count']  =  count($bookRst);
//     	//正在读=总数-未读-已读
//     	$bookGameRelationRst['now_read_count']  =  count($bookGameRstArr) - $bookGameRelationRst['not_read_count'] - $bookGameRelationRst['have_read_count'];
//     	return  $bookGameRelationRst;
    	$booklistGameRelation =  M('BooklistGameRelation');
    	$data	=	$booklistGameRelation->where($map)->group('isready')
    	->field('isready,count(*) cnt')->select();
//     	dump($data); 
    	return $data;
    }
    /**
     * 	@param $bookGameId  读书比赛ID
     * 	@param $classId     班级ID
     * 	班级积分前10在本次读书比赛中的排序
     * 	@return array     返回班级前10学生的总积分数组
     */
    public static function getScoreByClassId($bookGameId,$classId,$pagelist){
    	$class['classid']    =  $classId;
    	$class['status']     =  1;//正常状态
    	$class['isstudy']    =  1;//在读
    	$student             =  M('Student');
    	$students            =  $student->where($class)->select();
    	$allStudent          =  array_column($students, 'id');
    	$game['bgr.studentid']   =  array('in',$allStudent);
    	$game['bgr.book_game_id']=  $bookGameId;
    	$bookGameRelation    =  M('BookGameRelation bgr');
    	$rst = $bookGameRelation->join('primary_student s ON s.id=bgr.studentid')
    						    ->where($game)
    					        ->order('bgr.integral desc,bgr.have_read_count desc')
		    					->limit($pagelist)
		    					->select();
    	return $rst;
    }
    /**
     * 	获得比赛中的班级信息
     *  @param  $bookGameId    读书比赛ID 
     *  @return array          返回比赛所有班级信息二维数组
     */
    public static function getClassInfo($bookGameId){
    	$bookGame    = M('BookGame');
    	$bookGameRst = $bookGame->find($bookGameId);
    	$classids    = explode(',', $bookGameRst['classids']);
    	$classInfo   = M('ClassInfo');
    	$map['id']   = array('in',$classids);
    	$rst = $classInfo->where($map)->select();
    	return $rst;
    }
}